package com.example.fw;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.netbeans.jemmy.operators.JButtonOperator;
import org.netbeans.jemmy.operators.JDialogOperator;
import org.netbeans.jemmy.operators.JTextFieldOperator;
import org.netbeans.jemmy.operators.JTreeOperator;

import com.example.tests.Folders;

public class FolderHelper extends HelpersBase {

	public FolderHelper(ApplicationManager applicationManager) {
		super(applicationManager);
	}

	public Folders getFolder() {
		List<String> list = new ArrayList<String>();
		JTreeOperator tree = new JTreeOperator(mainFrame);
		Object[] children = tree.getChildren(tree.getRoot());
		for (Object child : children){
			list.add(""+child);
		}
		return new Folders(list);
	}
	
	public int selectFolder() {
		List<String> list 	= new ArrayList<String>();
		JTreeOperator tree 	= new JTreeOperator(mainFrame);		
		Object[] children 	= tree.getChildren(tree.getRoot());
		int indexFolder		= -1;
		Random rnd 			= new Random();

		for (Object child : children){
			list.add(""+child);
		}
		
		if (list.size() > 1){
			indexFolder = rnd.nextInt(list.size()-1);
			tree.selectRow(indexFolder);
		}
		return indexFolder;
	}

	public String createFolder(String folderName) {
		manager.getMenuHelper().pushCreateFolder(folderName);
		JDialogOperator dialog = new JDialogOperator(mainFrame);
		new JTextFieldOperator(dialog).setText(folderName);
		new JButtonOperator(dialog, "OK").push();
		return waitMessageDialog("Warning", 3000);
	}
	
	public int removeFolder(){
		int removalFolder = selectFolder();
		if ( -1 != removalFolder ){
			manager.getMenuHelper().pushDeleteFolder();
			JDialogOperator dialog = new JDialogOperator(mainFrame);
			new JButtonOperator(dialog, "Yes").push();
		}
		return removalFolder;
	}
}
